<?php
session_cache_expire(60);
session_start();

include ("Controle.class.php");

$controle = new Controle();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Formulário de Avaliação de Praças</title>
<link type="text/css" rel="stylesheet" media="screen"
	href="<?php echo dirname($_SERVER['PHP_SELF'])?>\..\css\main.css" />
</head>
<body>
<table width="100%" border="0">
	<tr>
		<td>
		<div id="wraper">
		<div id="content">
		<div id="container"><?php 
		$action = 'identificar';
		if (isset($_GET['action'])) $action = $_GET['action'];

		switch($action){
			case "identificar" :
				if (isset($_GET['error']) && $_GET['error']==1)
					include ("erro_usuario.php");
				elseif (isset($_GET['error']) && $_GET['error']==2)
					include ("erro_codigo.php");
				elseif (isset($_GET['error']) && $_GET['error']==3)
					include ("erro_senha.php");
				elseif (isset($_GET['error']) && $_GET['error']==4)
					include ("erro_cadastrar.php");
				include ("identificar.html");
				break;

			case "validar" :
				$matricula = $_POST['matricula'];
				
				$codigo = $_POST['codigo'];
				$codigo2 = $controle->getCodigo($matricula);

				if(!$codigo2){
					header("Location: index.php?action=identificar&error=1");
					exit;
				}
				
				$_SESSION['matricula'] = $matricula;
				
				if($codigo!=$codigo2){
					header("Location: index.php?action=identificar&error=2");
					exit;
				}
				
				$senha = $_POST['senha'];
				$senha2 = $_POST['senha2'];
				if($senha!=$senha2){
					header("Location: index.php?action=identificar&error=3");
					exit;
				}
				$_SESSION['senha'] = $senha;
				header("Location: index.php?action=cadastrar");
				exit;
				break;
					
			case "cadastrar" :
				$result = $controle->cadUsuario($_SESSION['matricula'], $_SESSION['senha']);
				if(!$result){
					header("Location: index.php?action=identificar&error=4");
					exit;
				}
				header("Location: index.php?action=fim");
				exit;
				break;
				
			case "fim" :
				include ("cadastrado.html");
				break;
		}
		?></div>
		</div>
		</div>
		</td>
	</tr>
</table>
</body>
</html>
